<?php defined('SYSPATH') or die('No direct access allowed.');

/**
 * theme
 */
class Model_Category extends ORM {

	protected $_has_many = array(
		'booklets'    => array('model' => 'booklet', 'through' => 'booklets_categories')
	);

	public function rules()
	{
		return array(
			'name' => array(
				array('not_empty'),
				array(array($this, 'name_available')),
			),
		);
	}

	public function name_available($name)
	{
		(bool) DB::select(array('COUNT("*")', 'total_count'))
			->from($this->_table_name)
			->where('name', '=', $name)
			->where($this->_primary_key, '!=', $this->pk())
			->execute($this->_db)
			->get('total_count');
	}

	public function get_image()
	{
		$booklet_array = $this->booklets->order_by("create_time", 'DESC')->limit(1)->find_all();
		foreach ($booklet_array as $booklet)
		{
			return $booklet->get_image();
		}
	}


} // End Category

